package com.example.fairplay.newfpregistration.ui;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.fairplay.newfpregistration.R;
import com.example.fairplay.newfpregistration.fragments.AboutFragment;
import com.example.fairplay.newfpregistration.fragments.BasicSystemFragment;
import com.example.fairplay.newfpregistration.fragments.DiapaylFragment;
import com.example.fairplay.newfpregistration.fragments.IntelSetttingFragment;
import com.example.fairplay.newfpregistration.util.StatusBarCompat;
import com.example.mylibrary.activity.BaseFragment;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by fairplay on 2018/5/31.
 */

public class SystemSettingActivity extends BaseActivity2 {
	@Bind(R.id.fragment_container)
	LinearLayout fragmentContainer;
	@Bind(R.id.btn_basic_setting)
	Button btnBasicSetting;
	@Bind(R.id.btn_intel_setting)
	Button btnIntelSetting;
	@Bind(R.id.btn_about)
	Button btnAbout;
	@Bind(R.id.btn_display)
	Button btnDisplay;
	private SystemSettingActivity mActivity;
	private Button[] mButtons;
	private int currentIndex = 0;
	private int index;
	private BasicSystemFragment basicSystemFragment;
	private IntelSetttingFragment intelSetttingFragment;
	private AboutFragment aboutFragment;
	private DiapaylFragment diapaylFragment;
	private BaseFragment[] fragments;
	@Bind(R.id.text_title)
	TextView tvTitle;
	@Bind(R.id.button_backward)
	Button btnBack;
	@Bind(R.id.btn_more)
	Button btnMore;

	@Override
	protected void onCreate(@Nullable Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_system_setting);
		StatusBarCompat.compat(this, R.color.blue);
		mActivity = this;
		ButterKnife.bind(this);
		initObject();
		initView();
		initData();
		setListener();

	}

	@Override
	protected void initData() {

	}

	@Override
	protected void initView() {
		tvTitle.setText("设置");

		mButtons = new Button[4];
		mButtons[0] = btnBasicSetting;
		mButtons[1] = btnIntelSetting;
		mButtons[2] = btnDisplay;
		mButtons[3] = btnAbout;


		basicSystemFragment = new BasicSystemFragment();
		intelSetttingFragment = new IntelSetttingFragment();
		diapaylFragment = new DiapaylFragment();
		aboutFragment = new AboutFragment();
		fragments = new BaseFragment[]{basicSystemFragment, intelSetttingFragment, diapaylFragment, aboutFragment};
		mButtons[0].setSelected(true);
		getSupportFragmentManager().beginTransaction().add(R.id.fragment_container, fragments[currentIndex]).show(basicSystemFragment).commit();


	}

	@Override
	protected void initObject() {

	}

	@Override
	protected void setListener() {
		btnBack.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				finish();
			}
		});
		for (final Button button : mButtons) {
			button.setOnClickListener(new View.OnClickListener() {
				@Override
				public void onClick(View view) {
					int id = view.getId();
					if (id == R.id.btn_basic_setting) {
						index = 0;
					} else if (id == R.id.btn_intel_setting) {
						index = 1;
					} else if (id == R.id.btn_display) {
						index = 2;
					} else if (id == R.id.btn_about) {
						index = 3;
					}
					if (currentIndex != index) {
						FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
						fragmentTransaction.hide(fragments[currentIndex]);
						if (fragments[index].isAdded()) {
							fragmentTransaction.show(fragments[index]).commit();
						} else {
							fragmentTransaction.add(R.id.fragment_container, fragments[index]).commit();
						}
						mButtons[currentIndex].setSelected(false);
						mButtons[index].setSelected(true);
					}
					currentIndex = index;
				}
			});
		}
	}


}
